interface IApi {
  appId: boolean
  description: string
  id: unknown
  method: string
  name: string
  tag: string
  url: string
}

/**
 * @description: 格式化api数据，用于树形组件使用
 * @param {IApi} apis
 * @param {*} disabled
 * @return {*}
 */
export const formatTreeApi = (apis: IApi[], disabled = false) => {
  const tags = [...new Set(apis.map(item => item.tag))]
  const tree = apis.map((api, i) => {
    api.id = tags.length + i
    return { ...api, disabled, id: api.id }
  })
  return tags.map((tag, index) => ({
    id: index,
    name: tag,
    tag,
    disabled,
    children: tree.filter(i => i.tag === tag),
  }))
}

// 格式化api列表用于饿了么树形组件使用
export const formatTree = (
  arr: any[], // api
  service: any[], // 服务
  disabled: boolean = false, // 是否支持修改
) => {
  let obj: any = {}
  let count = 0
  for (let i of arr) {
    let appId = i.appId ? i.appId : 'middleSystem'
    let tag = i.tag
    if (!obj[appId]) obj[appId] = {}
    if (!obj[appId][tag]) obj[appId][tag] = []
    i.disabled = disabled ? true : false
    i.id = ++count
    obj[appId][tag].push(i)
  }
  for (let i of service) {
    i.id = i.appId
    i.children = []
    i.disabled = disabled ? true : false
    Object.keys(obj).map(appId => {
      if (i.appId == appId) {
        Object.keys(obj[appId]).map(tag => {
          let item: any = {
            id: i.appId + tag + '',
            name: tag,
            children: obj[appId][tag],
            disabled: disabled ? true : false,
          }
          i.children.push(item)
        })
      }
    })
  }
  return service
}
